package com.nervenets.shopping.controller.order;

import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.exception.LogicException;
import com.nervenets.general.jwt.aspect.JwtRole;
import com.nervenets.general.web.BaseEntityController;
import com.nervenets.general.web.params.IdParams;
import com.nervenets.general.web.params.IdsParams;
import com.nervenets.shopping.controller.order.params.GroupBuyOrderFormParams;
import com.nervenets.shopping.controller.order.params.GroupBuyOrderPagingParams;
import com.nervenets.shopping.hibernate.domain.order.GroupBuyOrder;
import com.nervenets.shopping.hibernate.domain.order.GroupBuyOrderUser;
import com.nervenets.shopping.service.GroupBuyOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Slf4j
@Api(value = "拼团管理接口v1", tags = "拼团管理相关接口")
@RestController
@RequestMapping("/api/groupbuy/order")
@JwtRole(group = "orders", groupName = "订单管理", function = "groupbuy", functionName = "拼团管理", queue = 200)
public class GroupBuyOrderController extends BaseEntityController<GroupBuyOrder, GroupBuyOrderService, GroupBuyOrderFormParams, GroupBuyOrderPagingParams, GroupBuyOrder, GroupBuyOrder> {

    @Override
    public ResponseEntity<ResponseResult<GroupBuyOrder>> edit(@Valid @RequestBody GroupBuyOrderFormParams params, HttpServletRequest request, HttpServletResponse response) {
        throw new LogicException("拼团不支持编辑");
    }

    @Override
    public ResponseEntity<ResponseResult<Void>> del(@Valid @RequestBody IdsParams params, HttpServletRequest request, HttpServletResponse response) {
        throw new LogicException("拼团不支持删除");
    }

    @ApiOperation(value = "查询拼团数据")
    @PostMapping("/items")
    public ResponseEntity<ResponseResult<List<GroupBuyOrderUser>>> items(@Valid @RequestBody IdParams params) {
        GroupBuyOrder order = service.findOne(params.getTargetId(), "订单不存在");
        return successMessage(order.getUsers());
    }
}
